home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / CW GUSI 1.6.4 / Examples / GUSISocketTest.c < prev    next >
Text File  |  1995-09-20  |  6KB  |  325 lines

  1. /*********************************************************************
  2. File        :    GUSI                    -    Grand Unified Socket Interface
  3. File        :    GUSISocketTest.c    -    Socket testing gear
  4. Author    :    Matthias Neeracher <neeri@iis.ethz.ch>
  5. Language    :    MPW C
  6.  
  7. $Log: GUSISocketTest.c,v $
  8. Revision 1.2  1994/12/31  01:12:21  neeri
  9. ANSIfy.
  10. Roundtrip benchmark.
  11.  
  12. Revision 1.1  1994/02/25  02:47:47  neeri
  13. Initial revision
  14.  
  15. Revision 0.1  1992/09/08  00:00:00  neeri
  16. Factor out more common code
  17.  
  18. *********************************************************************/
  19.  
  20. #include <GUSI.h>
  21. #include "GUSITest.h"
  22. #include <Types.h>
  23. #include <stdio.h>
  24. #include <fcntl.h>
  25. #include <string.h>
  26. #include <sys/errno.h>
  27. #include <LowMem.h>
  28.  
  29. int    sock        =    -1;
  30. int    accsock    =    -1;
  31.  
  32. void Close(char ch1, char ch2, const char * cmd)
  33. {
  34.     if (close(sock))    {
  35.         printf("# close() returned error %s\n", Explain());
  36.         Where();
  37.     }
  38.     
  39.     sock        =    accsock;
  40.     accsock    =    -1;
  41. }
  42.  
  43. void Listen(char ch1, char ch2, const char * cmd)
  44. {
  45.     if (sock == -1)    {
  46.         printf("# socket is not open\n");
  47.         Where();
  48.         
  49.         return;
  50.     }
  51.  
  52.     if (listen(sock, 5))    {
  53.         printf("# listen() returned error %s\n", Explain());
  54.         Where();
  55.     }
  56. }
  57.  
  58. void Write(char ch1, char ch2, const char * line)
  59. {
  60.     int len    =    strlen(line);
  61.     int part;
  62.  
  63.     if (sock == -1)    {
  64.         printf("# socket is not open\n");
  65.         Where();
  66.             
  67.         return;
  68.     }
  69.     
  70.     for (; len; len -= part, line += part) {
  71.         part = write(sock, line, len);
  72.         if (part < 0)    {
  73.             printf("# write(\"%s\") returned error %s\n", line, Explain());
  74.             Where();
  75.             
  76.             break;
  77.         }        
  78.     }
  79. }
  80.  
  81. void Read(char ch1, char ch2, const char * cmd)
  82. {
  83.     int        len;
  84.     char         buf[500];
  85.     char *    line;
  86.     
  87.     if (sock == -1)    {
  88.         printf("# socket is not open\n");
  89.         Where();
  90.             
  91.         return;
  92.     }
  93.  
  94.     len    =    read(sock, buf, 500);
  95.     
  96.     if (len < 0)    {
  97.         printf("# read() returned error %s\n", Explain());
  98.     } else {
  99.         buf[len] = 0;
  100.         printf("# read() returned:\n");
  101.         
  102.         for (line = strtok(buf, "\n\r"); line; line = strtok(nil, "\n\r"))
  103.             printf("# %s\n", line);
  104.     }
  105.     
  106.     Where();
  107. }
  108.  
  109. void Select(char ch1, char ch2, const char * cmd)
  110. {
  111.     int                res;
  112.     fd_set            rdfds;
  113.     fd_set            wrfds;
  114.     fd_set            exfds;
  115.     struct timeval    delay;
  116.     
  117.     if (sock == -1)    {
  118.         printf("# socket is not open\n");
  119.         Where();
  120.             
  121.         return;
  122.     }
  123.     
  124.     FD_ZERO(&rdfds);
  125.     FD_ZERO(&wrfds);
  126.     FD_ZERO(&exfds);
  127.     
  128.     FD_SET(sock, &rdfds);
  129.     FD_SET(sock, &wrfds);
  130.     FD_SET(sock, &exfds);
  131.     
  132.     delay.tv_sec    =    10;
  133.     delay.tv_usec    =    0;
  134.     
  135.     res = select(sock+1, &rdfds, &wrfds, &exfds, &delay);
  136.     
  137.     if (res < 0)    {
  138.         printf("# select() returned error %s\n", Explain());
  139.     } else if (!res) {
  140.         printf("# select() timed out\n");
  141.     } else {
  142.         printf(
  143.             "# select() returned %s%s%s\n", 
  144.             FD_ISSET(sock, &rdfds) ? "canRead " : "",
  145.             FD_ISSET(sock, &wrfds) ? "canWrite " : "",
  146.             FD_ISSET(sock, &exfds) ? "exception " : "");
  147.     }
  148.     
  149.     Where();
  150. }
  151.  
  152. void TogBlk(char ch1, char ch2, const char * cmd)
  153. {
  154.     int    block;
  155.     
  156.     if (sock == -1)    {
  157.         printf("# socket is not open\n");
  158.         Where();
  159.             
  160.         return;
  161.     }
  162.     
  163.     switch (fcntl(sock, F_GETFL, 0))    {
  164.     case 0:
  165.         block    =    1;
  166.         break;
  167.     default:
  168.         block =     0;
  169.         break;
  170.     case -1:
  171.         printf("# fcntl(F_GETFL) returned error %s\n", Explain());
  172.         Where();
  173.         
  174.         return;
  175.     }
  176.     
  177.     if (ioctl(sock, FIONBIO, (long *) &block))
  178.         printf(
  179.             "# ioctl(FIONBIO, %s) returned error %s\n", 
  180.             block ? "true" : "false", 
  181.             Explain());
  182.     else
  183.         printf("# Socket is now %s\n", block ? "nonblocking" : "blocking");
  184.     
  185.     Where();
  186. }
  187.  
  188. void NRead(char ch1, char ch2, const char * cmd)
  189. {
  190.     int    nread;
  191.     
  192.     if (sock == -1)    {
  193.         printf("# socket is not open\n");
  194.         Where();
  195.             
  196.         return;
  197.     }
  198.     
  199.     if (ioctl(sock, FIONREAD, (long *) &nread))
  200.         printf("# ioctl(FIONREAD) returned error %s\n", Explain());
  201.     else
  202.         printf("# %d bytes waiting to be read\n", nread);
  203.     
  204.     Where();
  205. }
  206.  
  207. static char BenchBuf[8192];
  208.  
  209. void BenchSer(char ch1, char ch2, const char * cmd)
  210. {
  211.     char *    at;
  212.     int        requestSize;
  213.     int        responseSize;
  214.     int        packetSize;
  215.     int        count;
  216.     int        i;
  217.     
  218.     if (sock == -1)    {
  219.         printf("# socket is not open\n");
  220.         Where();
  221.             
  222.         return;
  223.     }
  224.     
  225.     at = BenchBuf;
  226.     
  227.     do {
  228.         at += read(sock, at, 1024);
  229.         *at = 0;
  230.     } while (!strchr(BenchBuf, '\015'));
  231.     
  232.     sscanf(BenchBuf, "%d %d %d\015", &requestSize, &responseSize, &count);
  233.     
  234.     write(sock, BenchBuf, 1);
  235.     
  236.     for (i=0; i++<count; ) {
  237.         packetSize = 0;
  238.         do { 
  239.             packetSize += read(sock, BenchBuf, 8192);
  240.         } while (packetSize < requestSize);
  241.         write(sock, BenchBuf, responseSize);
  242.     }
  243. }
  244.  
  245. void BenchCli(char ch1, char ch2, const char * cmd)
  246. {
  247.     char *    at;
  248.     int        requestSize;
  249.     int        responseSize;
  250.     int        packetSize;
  251.     int        contribSize;
  252.     int        count;
  253.     int        i;
  254.     long     startTime;
  255.     long    transTime;
  256.     Sampler    readSamp;
  257.     Sampler    writeSamp;
  258.     Sampler    sizeSamp;
  259.     
  260.     if (sock == -1)    {
  261.         printf("# socket is not open\n");
  262.         Where();
  263.             
  264.         return;
  265.     }
  266.  
  267.     if (sscanf(cmd, "%d %d %d", &requestSize, &responseSize, &count) != 3) {
  268.         Usage(ch1, ch2);
  269.         return;
  270.     }
  271.     
  272.     InitSampler(&readSamp);
  273.     InitSampler(&writeSamp);
  274.     InitSampler(&sizeSamp);
  275.     
  276.     write(sock, cmd, strlen(cmd)-1);
  277.     write(sock, "\015", 1);
  278.     read(sock, BenchBuf, 1);
  279.     
  280.     startTime = LMGetTicks();
  281.     
  282.     for (i=0; i++<count; ) {
  283.         transTime = LMGetTicks();
  284.         write(sock, BenchBuf, requestSize);
  285.         Sample(&writeSamp, LMGetTicks()-transTime);
  286.         packetSize = 0;
  287.         transTime = LMGetTicks();
  288.         do { 
  289.             contribSize = read(sock, BenchBuf, 8192);
  290.             packetSize += contribSize;
  291.             Sample(&sizeSamp, contribSize);
  292.         } while (packetSize < responseSize);
  293.         Sample(&readSamp, LMGetTicks()-transTime);
  294.     }
  295.     
  296.     printf("# Test took %d ticks.\n", LMGetTicks() - startTime);
  297.     printf("# Read  min: %2d max: %2d avg: %2.1f\n", 
  298.         readSamp.min, readSamp.max, ((double)readSamp.sum) / readSamp.count);
  299.     printf("# Size  min: %2d max: %2d avg: %2.1f\n", 
  300.         sizeSamp.min, sizeSamp.max, ((double)sizeSamp.sum) / sizeSamp.count);
  301.     printf("# Write min: %2d max: %2d avg: %2.1f\n", 
  302.         writeSamp.min, writeSamp.max, ((double)writeSamp.sum) / writeSamp.count);
  303. }
  304.  
  305. void AddSocketCommands()
  306. {
  307.     COMMAND('l', 'i', Listen,  "",                 "Listen to socket");
  308.     COMMAND('c', 'l', Close,      "",                 "Close socket");
  309.     COMMAND('w', 'r', Write,     "text",             "Write a line");
  310.     COMMAND('r', 'd', Read,      "",                 "Read");
  311.     COMMAND('s', 'e', Select,  "",                 "Select a socket");
  312.     COMMAND('t', 'b', TogBlk,  "",                 "Toggle blocking status");
  313.     COMMAND('n', 'r', NRead,    "",                "Number of bytes to be read");
  314.     COMMAND('b', 's', BenchSer,"",                "Benchmark, server side");
  315.     COMMAND('b', 'c', BenchCli,"reqSz resSz ct","Benchmark, client side");
  316. }
  317.  
  318. void CleanupSockets()
  319. {
  320.     if (sock != -1)
  321.         close(sock);
  322.     if (accsock != -1)
  323.         close(accsock);
  324. }
  325.